﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Photo_Entity : EntityBase<PHOTO>
    {
        public override RESP_Info Add(PHOTO model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into PHOTO(PHOTO_ID,PHOTO_NAME,PHOTO_URL,PHOTO_DES,PHOTO_HOT,PHOTO_COVER) values (@PHOTO_ID,@PHOTO_NAME,@PHOTO_URL,@PHOTO_DES,@PHOTO_HOT,@PHOTO_COVER)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_NAME", model.PHOTO_NAME));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_URL", model.PHOTO_URL));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_DES", model.PHOTO_DES));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_HOT", model.PHOTO_HOT));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_COVER", model.PHOTO_COVER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加照片成功</p></h5>";
                    info.Url = "PhotoList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加照片失败</p></h5>";
                    info.Url = "PhotoAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(PHOTO model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update PHOTO set PHOTO_NAME=@PHOTO_NAME,PHOTO_URL=@PHOTO_URL,PHOTO_DES=@PHOTO_DES,PHOTO_HOT=@PHOTO_HOT,PHOTO_COVER=@PHOTO_COVER where PHOTO_ID=@PHOTO_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", model.PHOTO_ID));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_NAME", model.PHOTO_NAME));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_URL", model.PHOTO_URL));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_DES", model.PHOTO_DES));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_HOT", model.PHOTO_HOT));
                    cmd.Parameters.Add(new SqlParameter("PHOTO_COVER", model.PHOTO_COVER));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑照片成功</p></h5>";
                    info.Url = "PhotoList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑照片失败</p></h5>";
                    info.Url = "PhotoAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(PHOTO photo)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from PHOTO where PHOTO_ID=@PHOTO_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", photo.PHOTO_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除照片成功</p></h5>";
                    info.Url = "PhotoList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除照片失败</p></h5>";
                    info.Url = "PhotoList.aspx";

                    Tom.Net.Log.LogManager.Error("删除照片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override PHOTO GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select PHOTO_ID,PHOTO_NAME,PHOTO_URL,PHOTO_DES,PHOTO_HOT,PHOTO_COVER from PHOTO where PHOTO_ID=@PHOTO_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PHOTO_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        PHOTO photo = new PHOTO();
                        if (!reader.IsDBNull(0)) photo.PHOTO_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) photo.PHOTO_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) photo.PHOTO_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) photo.PHOTO_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) photo.PHOTO_HOT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) photo.PHOTO_COVER = reader.GetInt16(5);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return photo;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override PHOTO GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        PHOTO photo = new PHOTO();
                        if (!reader.IsDBNull(0)) photo.PHOTO_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) photo.PHOTO_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) photo.PHOTO_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) photo.PHOTO_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) photo.PHOTO_HOT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) photo.PHOTO_COVER = reader.GetInt16(5);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return photo;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<PHOTO> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<PHOTO> photos = new List<PHOTO>();
                    while (reader.Read())
                    {
                        PHOTO photo = new PHOTO();
                        if (!reader.IsDBNull(0)) photo.PHOTO_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) photo.PHOTO_NAME = reader.GetString(1);
                        if (!reader.IsDBNull(2)) photo.PHOTO_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) photo.PHOTO_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) photo.PHOTO_HOT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) photo.PHOTO_COVER = reader.GetInt16(5);

                        photos.Add(photo);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return photos;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取照片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

